package Top200;

/**
 * @author zhangmin
 * @create 2022-02-21 15:52
 */
public class searchRange34 {
    int boundSearch(int[] nums,int target,boolean isLeft){
        int left=0,right=nums.length-1;
        while (left<=right){
            int mid=left+(right-left)/2;
            if (nums[mid]<target){
                left=mid+1;
            }else if (nums[mid]>target){
                right=mid-1;
            }else if (nums[mid]==target&&isLeft){
                right=mid-1;
            }else {
                left=mid+1;
            }
        }
        if (isLeft){
            if (left>=nums.length||nums[left]!=target) return -1;
            else return left;
        }else {
            if (right<0||nums[right]!=target) return -1;
            else return right;
        }

    }
    public int[] searchRange(int[] nums, int target) {
        return new int[]{boundSearch(nums,target,true),boundSearch(nums,target,false)};
    }
}
